package com.cmw.socket.l7.server;

import com.cmw.socket.constant.TCPConstants;
import com.cmw.socket.core.IoContext;
import com.cmw.socket.impl.IoSelectorProvider;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

@Slf4j
public class Server {

    public static void main(String[] args) throws IOException {
        IoContext.setup().ioProvider(new IoSelectorProvider()).start();
        TCPServer tcpServer = new TCPServer(TCPConstants.SERVER_PROT);
        boolean isSucceed = tcpServer.start();
        if(!isSucceed){
            log.info("Start TCP server failed!");
            return;
        }
        UDPProvider.start(TCPConstants.SERVER_PROT);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String str;
        do{
            str = bufferedReader.readLine();
            tcpServer.broadcast(str);
        }while (!"00bye00".equalsIgnoreCase(str));
        UDPProvider.stop();
        tcpServer.stop();
        IoContext.close();
    }
}
